
package com.maven.front.controller.frontSubject;

import com.maven.front.bean.EduCourseStudyhistory;
import com.maven.front.bean.Result;
import com.maven.front.service.frontSubject.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * @program: web-front
 * @description: 课程播放记录
 * @author: WangYiLong
 * @create: 2020-08-15 16:26
 **/

@RestController
@RequestMapping("user")
@Slf4j
public class FrontCourseStudyHistoryController {

    @Resource
    private VideoService videoService;

    @Resource
    private RedisTemplate redisTemplate;

//    @Resource
//    private RedissonClient redisson;
//
//    private int max = 2000;

    @PostMapping("addStudyHistory")
    public Result add(EduCourseStudyhistory courseStudyhistory){
        int num = videoService.updateStudyHistory(courseStudyhistory);
        if(num>0){
            return new Result(200,"成功");
        }else{
            return new Result(701,"播放记录存入失败");
        }
    }


    @GetMapping("getStudyHistory")
    public Result get(EduCourseStudyhistory courseStudyhistory){
        EduCourseStudyhistory eduCourseStudyhistory = videoService.getStudyHistory(courseStudyhistory);
        if(eduCourseStudyhistory!=null){
            log.info("*************"+eduCourseStudyhistory.getCurrentTime());
            return new Result(200,"成功",eduCourseStudyhistory);
        }
        return new Result(200,"查询失败");
    }

//    //单机锁
//    @PostMapping("subMaxTime")
//    public void sub(){
//       synchronized (this){
//           max--;
//       }
//
//    }
//
//    //redis 锁
//    @PostMapping("sub")
//    public void sub2(){
//        //不存在则创建,若存在，则表明当前有进程获得了此锁，返回，从而实现队列访问
//        Boolean flag = redisTemplate.opsForValue().setIfAbsent("numLock","");
//        if(!flag){
//            return ;
//        }
//        try{
//            String num = (String) redisTemplate.opsForValue().get("max");
//            int temp = Integer.valueOf(num);
//            redisTemplate.opsForValue().set("max",String.valueOf(temp-1));
//            System.out.println("当前库存："+redisTemplate.opsForValue().get("max"));
//        }finally { //一个进程执行完毕，删除该锁
//            redisTemplate.delete("numLock");
//        }
//    }
//
//    //redis redisson框架
//    @PostMapping("sub3")
//    public void sub3(){
//        RLock rLock = redisson.getLock("numLock");
//        rLock.lock(3, TimeUnit.SECONDS);
//        try{
//            String num = (String) redisTemplate.opsForValue().get("max");
//            int temp = Integer.valueOf(num);
//            redisTemplate.opsForValue().set("max",String.valueOf(temp-1));
//            System.out.println("当前库存："+redisTemplate.opsForValue().get("max"));
//        }finally { //一个进程执行完毕，删除该锁
//            rLock.unlock();
//        }
//    }
//
//    @PostMapping("print")
//    public String print(){
//        return (String) redisTemplate.opsForValue().get("max");
//    }
}

